package com.smartbone.compenent;

import java.io.IOException;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Calendar;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.servlet.ServletException;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import com.smartbone.compenent.base.BasePage;
import com.smartbone.dao.daoInterface.ILookupDAO;
import com.smartbone.entity.Authorities;
import com.smartbone.entity.Users;
import com.smartbone.services.interfaces.UserService;
import com.smartbone.util.Md5Encryption;
import com.smartbone.util.SecurityUtils;

@ManagedBean(name="userRegisteForm")  
@SessionScoped
public class UserRegisteForm extends BasePage implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2523204520730701097L;
	
	private boolean loginFlag;
	
	public boolean isLoginFlag() {
		
		if (SecurityUtils.isLogin()){
			loginFlag = true;
		}
		
		return loginFlag;
	}


	Users user = new Users();
	
	public Users getUser() {
		return user;
	}

	public void setUser(Users user) {
		this.user = user;
	}

	@Size(min=3, max=12)
	private String username;
	
	private String password;
	private String passwordCk;
	private String usernameCn;
	@Pattern(regexp = "^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[a-zA-Z]{2,4}$" , message="Bad email")
	private String mail;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPasswordCk() {
		return passwordCk;
	}

	public void setPasswordCk(String passwordCk) {
		this.passwordCk = passwordCk;
	}

	public String getUsernameCn() {
		return usernameCn;
	}

	public void setUsernameCn(String usernameCn) {
		this.usernameCn = usernameCn;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}


	@ManagedProperty("#{userService}")
	private UserService userService;

	@ManagedProperty("#{lookupDAO}")
	private ILookupDAO lookupDAO;
	
	public ILookupDAO getLookupDAO() {
		return lookupDAO;
	}

	public void setLookupDAO(ILookupDAO lookupDAO) {
		this.lookupDAO = lookupDAO;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	
	public String registeUserAction() throws IOException, ServletException {
		
		Calendar calendar = Calendar.getInstance();
		Timestamp timestamp = new Timestamp(calendar.getTimeInMillis());
		
    	user.setUsername(username);
    	user.setUsernameCn(usernameCn);
    	user.setMail(mail);
    	
		Authorities authorities = new Authorities();

		user.setPassword(Md5Encryption.encrypt32(password+"{"+username+"}"));
		// user type:0-administrator, 1-VIP, 2-normal user
		user.setType(1);

//		List<Lookup> lookupItemNoAr = lookupDAO.findByItemNo("AR01");
//		if (lookupItemNoAr != null && lookupItemNoAr.size() > 0) {
//			user.setLookupByArea(lookupItemNoAr.get(0));
//		}

		user.setEnabled(true);
		user.setLoginTime(timestamp);
		user.setAccountNonExpired(true);
		user.setAccountNonLocked(true);
		user.setCredentialsNonExpired(true);
		user.setCreateDate(timestamp);
		
		authorities.setAuthority("ROLE_USER");
		authorities.setUsers(user);
		authorities.setUsername(user.getUsername());

		user.getAuthoritieses().add(authorities);
		userService.registeUser(user);
		
		return "regSuccess";
		
	}

    public String edit() {
            
    	String userName = SecurityUtils.getUsername();
                
        if (userName != null) {

            Users loginedUser = userService.findByUserName(userName);
            setUser(loginedUser);
            this.setUsername(loginedUser.getUsername());
            this.setUsernameCn(loginedUser.getUsernameCn());
            this.setMail(loginedUser.getMail());
        }

        return "goEditUser";
    }
    
    public String updateUserAction() {

    	user.setUsername(username);
    	user.setUsernameCn(usernameCn);
    	user.setMail(mail);
    	user.setPassword(Md5Encryption.encrypt32(password+"{"+username+"}"));
        userService.updateUser(user);

        return "updateSuccess";
    }
}